//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  SettingBoard.m
//  iHeima
//
//  Created by he chao on 6/3/14.
//  Copyright (c) 2014 leon. All rights reserved.
//

#import "SettingBoard.h"
#import "FeedbackBoard.h"
#import "ChangePasswordBoard.h"

#pragma mark -

@interface SettingBoard()
{
	//<#@private var#>
}
@end

@implementation SettingBoard
DEF_SIGNAL(LOGOUT)
DEF_SIGNAL(LOGOUT_ALERT)

- (void)load
{
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    self.title = @"设置";
    [self showNaviBar];
    [self showBarButton:BeeUINavigationBar.LEFT image:IMAGESTRING(@"navi_back")];
    self.view.backgroundColor = RGB(246, 246, 246);
    [self loadContent];
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
    [self.stack popBoardAnimated:YES];
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

ON_SIGNAL2(SettingBoard, signal){
    if ([signal is:SettingBoard.LOGOUT]) {
//        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NO"];
//        [self.stack popToFirstBoardAnimated:YES];
    }
    else if ([signal is:SettingBoard.LOGOUT_ALERT]) {
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:nil message:@"您确认退出登录?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"退出", nil];
        [myAlert show];
        /*
        BeeUIAlertView *alert = [BeeUIAlertView spawn];
        alert.title = @"您确认退出登录?";
        [alert addCancelTitle:@"取消"];
        [alert addButtonTitle:@"退出" signal:SettingBoard.LOGOUT];
        [alert showInViewController:self];*/
    }
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        //添加APP下载页
        if (alertView.tag == 9528) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:kStoreUrl]];
            return;
        }
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"hasLogin"];
        MainBoard *board = [MainBoard sharedInstance];
        [board showMainActivity];
        
        [self.stack popToFirstBoardAnimated:YES];
    }
}

- (void)loadContent{
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.viewWidth, self.viewHeight-(IOS7_OR_LATER?64:44))];
    myTableView.backgroundColor = [UIColor clearColor];
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];
    
    UIView *viFooter = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewWidth, 120)];
    
    BaseButton *btnLogout = [BaseButton initBaseBtn:IMAGESTRING(@"logout") highlight:IMAGESTRING(@"logout_pre") text:@"退出登录" textColor:[UIColor whiteColor] font:FONT(15)];
    btnLogout.frame = CGRectMake(30, 20, 260, 44);
    [btnLogout addSignal:SettingBoard.LOGOUT_ALERT forControlEvents:UIControlEventTouchUpInside];
    [viFooter addSubview:btnLogout];
    
    BaseLabel *lbVersion = [BaseLabel initLabel:@"版本V1.0" font:FONT(14) color:kNormalColor textAlignment:NSTextAlignmentCenter];
    lbVersion.frame = CGRectMake(0, btnLogout.bottom+10, self.viewWidth, 20);
    [viFooter addSubview:lbVersion];
    
    myTableView.tableFooterView = viFooter;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewWidth, 20)];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
        
        //[cell initSelf];
    }
    
    for (id element in cell.contentView.subviews) {
        [element removeFromSuperview];
    }
    NSString *strTitle;
    
  //  NSArray *arrayT = @[@"检查更新",@"清除缓存",@"修改密码",@"意见反馈"];
    
    NSArray *arrayT = @[@"检查更新",@"清除缓存",@"修改密码"];
    strTitle = arrayT[indexPath.row];
    cell.textLabel.text = strTitle;
    cell.backgroundColor = [UIColor whiteColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.section == 0 && indexPath.row<3) {
        BeeUIImageView *line = [BeeUIImageView spawn];
        line.frame = CGRectMake(0, 43.5, self.viewWidth, 0.5);
        line.backgroundColor = RGB(187, 187, 187);
        [cell.contentView addSubview:line];
    }
    //[cell load];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
        {
            
           UIAlertView* av  =[[UIAlertView alloc]initWithTitle:@"版本已经最新" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [av show];
            
            
           // [[BeeUITipsCenter sharedInstance] presentLoadingTips:@"正在检查更新"];
//            [self POST:[NSString stringWithFormat:@"%@/apiapp/checkupdate",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"session_id",kUserInfo[@"session_id"]).PARAM(@"type",@"1").PARAM(@"version",@"1");
        }
            break;
        case 1:
        {
            [[BeeUITipsCenter sharedInstance] presentLoadingTips:@"正在清除缓存"];
            [[SDImageCache sharedImageCache] cleanDisk];
            [[SDImageCache sharedImageCache] clearDisk];
            [self performSelector:@selector(clearCacheSuccess) withObject:nil afterDelay:2];
        }
            break;
        case 2:
        {
            ChangePasswordBoard *board = [[ChangePasswordBoard alloc] init];
            [self.stack pushBoard:board animated:YES];
        }
            break;
//        case 3:
//        {
//            FeedbackBoard *board = [[FeedbackBoard alloc] init];
//            [self.stack pushBoard:board animated:YES];
//        }
//            break;
            
        default:
            break;
    }
}

- (void)clearCacheSuccess{
    [[BeeUITipsCenter sharedInstance] presentMessageTips:@"清除缓存成功"];
}

- (void)handleRequest:(BeeRequest *)request
{
    if(request.failed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        NETWORK_ERROR
    }
    else if (request.succeed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        id json = [request.responseString mutableObjectFromJSONString];
        NSLog(@"检测版本JSON:%@",json);
        switch ([json[@"status"] intValue]) {
            case 1:
            {
                if ([[NSString stringWithFormat:@"%@",json[@"res"][@"version"]] isEqualToString:@"1.0.2"]) {
                    [[BeeUITipsCenter sharedInstance] presentMessageTips:@"您的版本已经是最新的"];
                }
                else {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"敬请期待" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                    alertView.tag = 9528;
                    [alertView show];
                    
                    

                }
            }
                break;
        }
    }
}



@end
